<template>
  <div id="login_container">
    <div id="login_box">
      <h2>lOGIN</h2>
      <el-form
        :model="loginFrom"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        :label-position="labelPosition"
        id="form"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="loginFrom.username"
            clear="in"
            type="text"
            placeholder="username"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginFrom.password"
            clear="in"
            type="password"
            placeholder="password"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >Sign in</el-button
          >
          <el-button type="info" @click="resetForm('ruleForm')"
            >Remake</el-button
          >
        </el-form-item>
      </el-form>
      <br />
      <div id="Sign">
        <a href="a">忘记密码?</a>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      labelPosition: 'top',
      loginFrom: {
        username: 'admin',
        password: '123456',
      },
      timer: null, //防抖时间
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
    }
  },
  methods: {
    submitForm(formName) {
      //防抖处理
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            const { data: res } = await this.$http.post('login', this.loginFrom)
            if (!res.data) {
              this.$message.error('账号或者密码错误')
            } else {
              window.sessionStorage.setItem('token', res.data.token)
              this.$message.success('登录成功')
              this.$router.push('/home')
            }
          }
        })
      }, 500)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
#login_container {
  position: relative;
  height: 100%;
  background-image: url('../assets/image/login/0.jpeg');
  background-size: cover;
  background-repeat: no-repeat;
}
#login_box {
  position: absolute;
  width: 20%;
  height: 400px;
  background: #00000040;
  margin: auto;
  text-align: center;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  padding: 50px 50px;
}

#login_box h2 {
  color: #e8e7b730;
  margin-top: 5%;
}

#login_box #form #input_box {
  margin-top: 5%;
}

#login_box #form #input_box i {
  color: #e8e7b730;
}

#login_box #form #input_box input {
  border: 0;
  width: 60%;
  font-size: 15px;
  color: #fff;
  background: #ffffff00;
  border-bottom: 2px solid#eacda950;
  padding: 5px 10px;
  margin-top: 10px;
}

#login_box button {
  margin-top: 50px;
  //   border: 0;
  color: #fff;
}

#login_box #Sign {
  margin-left: 60%;
}

#login_box #Sign a {
  color: #d1c2d8;
}
</style>
